<?php defined('SYSPATH') or die('No direct script access.');

class Controller_Search extends Controller_Template {

    public function before()
    {
        parent::before();
        
        if ($this->auto_render === TRUE)
        {
            $this->title = '搜索结果';
            $this->navigate->add('搜索结果');
            
            $this->template->content_right(hook('sidebar'));
        }
    }

    public function after()
    {
        if ($this->auto_render === TRUE)
        {
        	;
        }
        
        parent::after();
    }
    
	public function action_index()
	{
		if (isset($this->get['search_k']) && ($keywords = $this->get['search_k'])){
			setcookie('search_k',urldecode($keywords));
			
			$searcher = lib_search::instance('article')
			->find($keywords)
			->in('title','rewrite','content');
			
			$this->pagination = new Pagination(array(
				'total_items'=>$searcher->num_rows()
			));
			
			$list = $searcher->offset($this->pagination->offset)
			->limit($this->pagination->items_per_page)
			->order_by('addTime','desc')->exec();
		}else{
			$list = MON::factory('article')->find();
			
			$this->pagination = new Pagination(array(
				'total_items'=>$list->count()
			));
			
			$list
			->skip($this->pagination->offset)
			->limit($this->pagination->items_per_page)
			->sort(array('addTime'=>-1));
		}
		
		$this->content = View::factory('index/default')
		->set('list',$list);
	}
} // End Welcome
